package com.sjz.myfrist;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

/***
 *计步器
 */
public class SensorActivity4 extends AppCompatActivity implements SensorEventListener {

    private SensorManager mSensorManager;
    private TextView mStepCounterTextView, mStepDetectorTextView; // Textview for showing steps counter of respective sensors
    private boolean isActivityRunning; // if true then activity is in foreground
    private int mStepDetectCounter = 0; // used in step detector sensor


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sensor_demo_four_id);

        PackageManager packageManager = getPackageManager();
        if (packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_STEP_COUNTER)
                && packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_STEP_DETECTOR)) {
            mStepCounterTextView = (TextView) findViewById(R.id.step_counter);
            mStepDetectorTextView = (TextView) findViewById(R.id.step_detector);
            mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        } else {
            Toast.makeText(this, "This phone does not supports required sensor", Toast.LENGTH_SHORT).show();
        }

        if(ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACTIVITY_RECOGNITION) == PackageManager.PERMISSION_DENIED){
            //ask for permission
            requestPermissions(new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, 1);
        }
    }
    @Override
    public void onSensorChanged(SensorEvent event) {
        switch (event.sensor.getType()) {
            case Sensor.TYPE_STEP_COUNTER:
                if (isActivityRunning) {
                    mStepCounterTextView.setText(String.valueOf(event.values[0]));
                }
                break;
            case Sensor.TYPE_STEP_DETECTOR:
                if (isActivityRunning) {
                    mStepDetectCounter++;
                    mStepDetectorTextView.setText(String.valueOf(mStepDetectCounter));
                }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    @Override
    protected void onResume() {
        super.onResume();

        isActivityRunning = true;
        Sensor countSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
        Sensor detectSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
        if (countSensor != null) {
            mSensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_FASTEST);
        } else {
            Toast.makeText(this, "Count sensor not available!", Toast.LENGTH_LONG).show();
        }
        if (detectSensor != null) {
            mSensorManager.registerListener(this, detectSensor, SensorManager.SENSOR_DELAY_FASTEST);
        } else {
            Toast.makeText(this, "Count sensor not available!", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        isActivityRunning = false;
    }

    @Override
    protected void onStop() {
        super.onStop();
        mSensorManager.unregisterListener(this);
    }


}